package com.basin.common.mongo.config;

import com.anwen.mongo.cache.global.ExecutorReplacerCache;
import com.anwen.mongo.config.MongoPlusAutoConfiguration;
import com.anwen.mongo.logic.replacer.LogicRemoveReplacer;
import com.anwen.mongo.model.LogicProperty;
import org.springframework.context.annotation.Configuration;

/**
 * 预计2.1.5修复内置removeById错误
 * https://blog.csdn.net/lovexinxin_/article/details/141869555?sharetype=blogdetail&shareId=141869555&sharerefer=APP&sharesource=lovexinxin_&sharefrom=link
 *
 * @author: Sam ZHONG | sammy1997@aliyun.com
 */
@Configuration
public class LogicConfiguration {
    private final LogicProperty logicProperty;

    public LogicConfiguration(LogicProperty logicProperty, MongoPlusAutoConfiguration ignoredMongoPlusAutoConfiguration) {
        this.logicProperty = logicProperty;
        setLogic();
    }

    private void setLogic() {
        // 是否开启逻辑删除
        if (logicProperty.getOpen()) {
            // 添加执行替换器缓存组件为CustomLogicRemoveReplacer类
            ExecutorReplacerCache.replacers.removeIf(next -> next instanceof LogicRemoveReplacer);
            ExecutorReplacerCache.replacers.add(new CustomLogicRemoveReplacer());
            // 替换器排序
            ExecutorReplacerCache.sorted();
        }
    }
}
